//
//  EmotionDeatilViewController.m
//  Games
//
//  Created by iOS Three on 14/12/31.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "EmotionDeatilViewController.h"
#import "UIImage+GIF.h"
#import "NSData+GIF.h"
#import <ShareSDK/ShareSDK.h>
@interface EmotionDeatilViewController ()

@end

@implementation EmotionDeatilViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 20)];
    label.text = _gifName;
    label.font = [UIFont fontWithName:@"MFYaYuan_Noncommercial-Regular" size:20];
    label.textColor = [UIColor colorWithRed:0.98 green:0.59 blue:0.16 alpha:1];
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = label;
    
    // Do any additional setup after loading the view.
}
-(void)removeShare {
    if ([_share superview]) {
        [UIView animateWithDuration:0.5 animations:^{
            if (kwidth == 320) {
                if (kHeight == 480) {
                    _share.frame = CGRectMake(5, kHeight, kwidth - 10, 220);
                } else {
                    _share.frame = CGRectMake(5, kHeight, kwidth - 10, 293);
                }
            } else if (kwidth == 414) {
                _share.frame = CGRectMake(5, kHeight, kwidth - 10, 350);
            }
        } completion:^(BOOL finished) {
            [_back removeFromSuperview];
        }];
    }
}
-(void)getData {
    if ([_gifName isEqualToString:@"阿狸"]|[_gifName isEqualToString:@"丘比猫"]|[_gifName isEqualToString:@"小幺鸡"]) {
        _isPath = YES;
    } else {
        _isPath = NO;
    }
    _gifNum = 0;
    _gifArray = [[NSMutableArray alloc]init];
    _gifNameArray = [[NSMutableArray alloc]init];
    _gifUrlArray = [[NSMutableArray alloc]init];
    _gifDataArray = [[NSMutableArray alloc]init];
    if ([_gifName isEqualToString:@"阿狸"]) {
        _gifArray = [NSMutableArray arrayWithObjects:@"下雪了",@"不要迷恋哥",@"你是好人",@"你是白痴",@"你死定了",@"你真强",@"偷偷看你",@"光棍",@"再见",@"吻",@"国庆",@"媳妇儿",@"害羞",@"平凡的人类",@"开心",@"开饭",@"感恩的心",@"我要女朋友",@"抱抱",@"无聊",@"无语",@"气死你",@"浇花",@"生气",@"约会",@"表欺负我",@"送礼",@"送花",@"那个那个", nil];
    } else if ([_gifName isEqualToString:@"丘比猫"]) {
        _gifArray = [NSMutableArray arrayWithObjects:@"意淫中",@"俯卧撑",@"讨好",@"不开心",@"好香啊",@"惬意",@"想",@"感冒鼻涕",@"踩",@"抱歉",@"敲鼓",@"疑问",@"鄙视",nil];
    } else if ([_gifName isEqualToString:@"小幺鸡"]) {
        _gifArray = [NSMutableArray arrayWithObjects:@"交际舞",@"优雅",@"剁腿",@"吻腿",@"唱歌",@"嘚瑟",@"嚎叫",@"妖娆",@"妩媚",@"小苹果",@"小龙女",@"开枪",@"怎么会",@"怒打",@"我弹我弹",@"受伤",@"扭秧歌",@"抢劫",@"抽风",@"无辜",@"早上好",@"晕",@"晕头转向",@"杀",@"梦露",@"游泳",@"激动",@"炫富",@"犯傻",@"糗大了",@"美洋洋",@"蒙面",@"装逼",@"跳芭蕾",@"蹦蹦跳",@"蹭墙",@"钢管舞",@"铁头功",@"飙泪",@"飞飞",nil];
    }
    for (int i = 0; i < _gifArray.count; i++) {
        NSString *path = [[NSBundle mainBundle] pathForResource:[_gifArray objectAtIndex:i] ofType:@"gif"];
        NSData *gifData = [NSData dataWithContentsOfFile:path];
        [_gifUrlArray addObject:path];
        [_gifDataArray addObject:gifData];
    }
    [self creatController];
}
-(void)creatController {
    UIImageView *imageBack = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kwidth, kHeight)];
    imageBack.image = [UIImage imageNamed:@"di6_02@3x"];
    [self.view addSubview:imageBack];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:[_gifArray objectAtIndex:0] ofType:@"gif"];
    NSData *gifData = [NSData dataWithContentsOfFile:path];
    _image = [[UIImageView alloc]init];
    _image.backgroundColor = [UIColor clearColor];
    _image.image = [UIImage sd_animatedGIFWithData:gifData];
    [self.view addSubview:_image];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[UIImage imageNamed:@"fasong_03"] forState:UIControlStateNormal];
    button.tag = 0;
    [self.view addSubview:button];
    
    static int index;
    static int row;
    if (kwidth == 320) {
        index = 6;
        row = 3;
    } else if (kwidth == 414) {
        index = 12;
        row = 4;
    } else if (kwidth == 375) {
        index = 9;
        row = 3;
    }
    if (_gifArray.count % index != 0) {
        _x = _gifArray.count / index + 1;
    } else {
        _x = _gifArray.count / index;
    }
    _scroll = [[UIScrollView alloc]init];
    _scroll.delegate = self;
    _scroll.showsHorizontalScrollIndicator = NO;
    _scroll.pagingEnabled = YES;
    [self.view addSubview:_scroll];
    
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(_scroll.center.x-50, kHeight-10, 100, 9)];
    _pageControl.numberOfPages = _x;
    _pageControl.currentPage = 0;
    _pageControl.userInteractionEnabled = YES;
    _pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:0.96 green:0.56 blue:0.15 alpha:1];
    _pageControl.pageIndicatorTintColor = [UIColor colorWithRed:0.91 green:0.75 blue:0.45 alpha:1];
    [_pageControl addTarget:self action:@selector(pageClickChangeScroll:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_pageControl];
    
    if (kwidth == 320) {
        if (kHeight == 480) {
            _image.frame = CGRectMake(44, 25 + 64, 125, 125);
            _scroll.frame = CGRectMake(0, 173 + 64, kwidth, 223);
            _scroll.contentSize = CGSizeMake(kwidth * _x, 223);
            _pageControl.center = CGPointMake(_scroll.center.x, kHeight-10);
            button.frame = CGRectMake(200, 110, 87, 87);
        } else {
            _image.frame = CGRectMake(10, 84, 164, 164);
            button.frame = CGRectMake(200, 110, 87, 87);
            _scroll.frame = CGRectMake(0, kHeight - 293, kwidth, 293);
            _scroll.contentSize = CGSizeMake(kwidth * _x, 293);
            _pageControl.center = CGPointMake(_scroll.center.x, kHeight-58);
        }
    } else if (kwidth == 414) {
        _image.frame = CGRectMake(56, 108, 164, 164);
        button.frame = CGRectMake(252, 62 + 64, 124, 124);
        _scroll.frame = CGRectMake(0, 317, kwidth, 340);
        _scroll.contentSize = CGSizeMake(kwidth * _x, 340);
        _pageControl.center = CGPointMake(_scroll.center.x, kHeight - 47.5);
    } else if (kwidth == 375) {
        _image.frame = CGRectMake(52.5, 28+64, 147.5, 147.5);
        button.frame = CGRectMake(226, 100.5, 124, 124);
        _scroll.frame = CGRectMake(0, 257.5, kwidth, 391);
        _scroll.contentSize = CGSizeMake(kwidth * _x, 391);
        _pageControl.center = CGPointMake(_scroll.center.x, kHeight - 10);
    }
    int onePageNum;
    int viewTag = 0;
    for (int i = 0; i < _x; i++) {
        if (i == _gifArray.count / index) {
            onePageNum = _gifArray.count % index;
        } else {
            onePageNum = index;
        }
        for (int j = 0; j < onePageNum; j++) {
            UIView *back = [[UIView alloc]init];
            back.tag = viewTag;
            back.backgroundColor = [UIColor whiteColor];
            if (kwidth == 320) {
                if (j < row) {
                    back.frame = CGRectMake(18 + i * kwidth + j * (78 + 24), 0, 78, 101);
                } else {
                    int k = j - row;
                    back.frame = CGRectMake(18 + i * kwidth + k * (78 + 24), 122, 78, 101);
                }
            } else if (kwidth == 414) {
                if (j < row) {
                    back.frame = CGRectMake(23 + i * kwidth + j * (75 + 23), 0, 75, 99);
                } else if (j < row * 2){
                    int k = j - row;
                    back.frame = CGRectMake(23 + i * kwidth + k * (75 + 23), 120, 75, 99);
                } else {
                    int k = j - row * 2;
                    back.frame = CGRectMake(23 + i * kwidth + k * (75 + 23), 120 + 120, 75, 99);
                }
            } else if (kwidth == 375) {
                if (j < row) {
                    back.frame = CGRectMake(22 + i * kwidth + j * (90 + 29), 0, 90, 117);
                } else if (j < row * 2){
                    int k = j - row;
                    back.frame = CGRectMake(22 + i * kwidth + k * (90 + 29), 137, 90, 117);
                } else {
                    int k = j - row * 2;
                    back.frame = CGRectMake(22 + i * kwidth + k * (90 + 29), 137 * 2, 90, 117);
                }
            }
            [back addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewClick:)]];
            [_scroll addSubview:back];
            
            UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(3, 2, 68, 68)];
            if (_isPath) {
                image.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.gif",[_gifArray objectAtIndex:viewTag]]];
            } else {
                image.image = [UIImage imageNamed:[_gifArray objectAtIndex:viewTag]];
            }
            [back addSubview:image];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(3, 75, 68, 25)];
            label.text = [_gifArray objectAtIndex:viewTag];
            label.textColor = [UIColor colorWithRed:0.96 green:0.55 blue:0.15 alpha:1];
            label.font = [UIFont fontWithName:@"MFYaYuan_Noncommercial-Regular" size:11];
            label.textAlignment = NSTextAlignmentCenter;
            [back addSubview:label];
            
            UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 74, 75, 1)];
            line.image = [UIImage imageNamed:@"xuxian_09"];
            [back addSubview:line];
            
            if (kwidth == 320) {
                image.frame = CGRectMake(3, 3.5, 70, 70);
                line.frame = CGRectMake(0, 75.5, 78, 1);
                label.frame = CGRectMake(0, 76.5, 78, 22.5);
                label.font = [UIFont fontWithName:@"MFYaYuan_Noncommercial-Regular" size:14];
            } else if (kwidth == 375) {
                image.frame = CGRectMake(4, 4, 82, 82);
                line.frame = CGRectMake(0, 88, 90, 1);
                label.frame = CGRectMake(0, 89, 90, 29);
                label.font = [UIFont fontWithName:@"MFYaYuan_Noncommercial-Regular" size:15];
            }
            viewTag++;
        }
    }
}
-(void)viewClick:(UITapGestureRecognizer *)tap {
    _gifNum = tap.view.tag;
    NSString *path = [[NSBundle mainBundle] pathForResource:[_gifArray objectAtIndex:tap.view.tag] ofType:@"gif"];
    NSData *gifData = [NSData dataWithContentsOfFile:path];
    _image.image = [UIImage sd_animatedGIFWithData:gifData];
}
-(void)pageClickChangeScroll:(UIPageControl *)aPageControl {
    _scroll.contentOffset = CGPointMake(aPageControl.currentPage * kwidth, 0);
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    _pageControl.currentPage = scrollView.contentOffset.x / kwidth;
}
-(void)buttonClick:(UIButton *)sender {
    if (sender.tag == 0) {
        _back = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kwidth, kHeight)];
        _back.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [_back addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeShare)]];
        [self.view addSubview:_back];
        
        _share = [[UIView alloc]initWithFrame:CGRectMake(5, kHeight, kwidth - 10, 220)];
        _share.frame = CGRectMake(5, kHeight, kwidth - 10, 220);
        _share.backgroundColor = [UIColor whiteColor];
        _share.layer.cornerRadius = 10;
        [self.view addSubview:_share];
        
        UIButton *weiChat = [UIButton buttonWithType:UIButtonTypeCustom];
        [weiChat setBackgroundImage:[UIImage imageNamed:@"share_12"] forState:UIControlStateNormal];
        weiChat.frame = CGRectMake(30, 10, 97, 97);
        weiChat.tag = 0;
        [weiChat addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
        [_share addSubview:weiChat];
        
        UIButton *weiXin = [UIButton buttonWithType:UIButtonTypeCustom];
        [weiXin setBackgroundImage:[UIImage imageNamed:@"share_03"] forState:UIControlStateNormal];
        weiXin.frame = CGRectMake(_share.frame.size.width - 30 - 97, 10, 97, 97);
        weiXin.tag = 1;
        [weiXin addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
        [_share addSubview:weiXin];
        
        UIButton *qq = [UIButton buttonWithType:UIButtonTypeCustom];
        [qq setBackgroundImage:[UIImage imageNamed:@"share_05"] forState:UIControlStateNormal];
        qq.frame = CGRectMake(30, 117, 97, 97);
        qq.tag = 2;
        [qq addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
        [_share addSubview:qq];
        
        UIButton *sina = [UIButton buttonWithType:UIButtonTypeCustom];
        [sina setBackgroundImage:[UIImage imageNamed:@"share_10"] forState:UIControlStateNormal];
        sina.frame = CGRectMake(_share.frame.size.width - 30 - 97, 117, 97, 97);
        sina.tag = 3;
        [sina addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
        [_share addSubview:sina];
        
        if (kwidth == 414) {
            weiChat.frame = CGRectMake(50, 10, 97, 97);
            weiXin.frame = CGRectMake(_share.frame.size.width - 50 - 97, 10, 97, 97);
            qq.frame = CGRectMake(50, 117, 97, 97);
            sina.frame = CGRectMake(_share.frame.size.width - 50 - 97, 117, 97, 97);
        }
        
        [UIView animateWithDuration:0.5 animations:^{
            _share.frame = CGRectMake(5, kHeight - 220, kwidth - 10, 220);
        } completion:^(BOOL finished) {
            
        }];
    } else {
        
    }
}
-(void)share:(UIButton *)button {
    id<ISSCAttachment> image;
    if (_isPath) {
        image = [ShareSDK imageWithPath:[_gifUrlArray objectAtIndex:_gifNum]];
    } else {
        image = [ShareSDK imageWithUrl:[_gifUrlArray objectAtIndex:_gifNum]];
    }
    if (button.tag == 0) {
        //构造分享内容
        id<ISSContent> publishContent = [ShareSDK content:@"这是在分享图片,这个不显示的"
                                           defaultContent:@"测试一下分享Gif图片"
                                                    image:image
                                                    title:@"ShareSDK"
                                                      url:@"http://www.mob.com"
                                              description:@"这是一条测试信息"
                                         //注意此分享Gif的类型只适用于微信
                                                mediaType:SSPublishContentMediaTypeGif];
        //定制微信好友信息
        [publishContent addWeixinSessionUnitWithType:[NSNumber numberWithInteger:
                                                      SSPublishContentMediaTypeGif]
                                             content:@"这是在分享图片,这个不显示的"
                                               title: @"Hello 微信好友!"
                                                 url:@"http://www.mob.com"
                                               image:image
                                        musicFileUrl:nil
                                             extInfo:nil
                                            fileData:nil
                                        emoticonData:[_gifDataArray objectAtIndex:_gifNum]];
        [ShareSDK shareContent:publishContent
                          type:ShareTypeWeixiSession
                   authOptions:nil
                  shareOptions:nil
                 statusBarTips:YES
                        result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                            if (state == SSPublishContentStateSuccess){
                                NSLog(NSLocalizedString(@"TEXT_SHARE_SUC", @"发表成功"));
                            } else if (state == SSPublishContentStateFail) {
                                NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"发布失败!error code == %d, error code == %@"), [error errorCode], [error errorDescription]);
                            }
                        }];
    } else if (button.tag == 1) {
        //发送内容给微信
        id<ISSContent> content = [ShareSDK content:nil
                                    defaultContent:nil
                                             image:image
                                             title:nil
                                               url:nil
                                       description:nil
                                         mediaType:SSPublishContentMediaTypeImage];
        [ShareSDK shareContent:content
                          type:ShareTypeWeixiTimeline
                   authOptions:nil
                 statusBarTips:YES
                        result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                            if (state == SSPublishContentStateSuccess) {
                                NSLog(@"success");
                                [self removeShare];
                            } else if (state == SSPublishContentStateFail) {
                                if ([error errorCode] == -22003) {
                                    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"TEXT_TIPS", @"提示") message:[error errorDescription] delegate:nil cancelButtonTitle:NSLocalizedString(@"TEXT_KNOW", @"知道了") otherButtonTitles:nil];
                                    [alertView show];
                                }
                            }
                        }];
    } else if (button.tag == 2) {
        id<ISSContent> publishContent = [ShareSDK content:@"这是在分享图片,这个不显示的"
                                           defaultContent:@""
                                                    image:image
                                                    title:@"ShareSDK"
                                                      url:@"http://www.mob.com"
                                              description:@"这是一条测试信息"
                                         //注意QQ好友分享gif图片要使用image类型的
                                                mediaType:SSPublishContentMediaTypeImage];
        [ShareSDK shareContent:publishContent
                          type:ShareTypeQQ
                   authOptions:nil
                  shareOptions:nil
                 statusBarTips:YES
                        result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                            if (state == SSPublishContentStateSuccess){
                                NSLog(NSLocalizedString(@"TEXT_SHARE_SUC", @"发表成功"));
                            } else if (state == SSPublishContentStateFail) {
                                NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"发布失败!error code == %d, error code == %@"), [error errorCode], [error errorDescription]);
                            }
                        }];
    } else if (button.tag == 3) {
        id<ISSContent> publishContent = [ShareSDK content:@"这是在分享图片,这个不显示的"
                                           defaultContent:@"测试一下分享Gif图片"
                                                    image:image
                                                    title:@"ShareSDK"
                                                      url:@"http://www.mob.com"
                                              description:@"这是一条测试信息"
                                         //注意此分享Gif的类型只适用于微信
                                                mediaType:SSPublishContentMediaTypeImage];
        //自定义UI分享
        [ShareSDK shareContent:publishContent
                          type:ShareTypeSinaWeibo
                   authOptions:nil
                  shareOptions:nil
                 statusBarTips:YES
                        result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                            if (state == SSPublishContentStateSuccess) {
                                NSLog(NSLocalizedString(@"TEXT_SHARE_SUC", @"发表成功"));
                                [self alertViewShow:@"分享成功"];
                            } else if (state == SSPublishContentStateFail) {
                                NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"发布失败!error code == %d, error code == %@"), [error errorCode], [error errorDescription]);
                            }
                        }];
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    [self removeShare];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
